package com.fs.ibox.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class BeanFactory {
    static Properties props = new Properties();
    static{
        //加载db.properties文件
        InputStream in =  JDBCUtil.class.getClassLoader().getResourceAsStream("bean.properties");
        try {
            //加载到Properties集合中
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 产生一个Dao的实现类对象
     */
    public static <T>  T  createBean(Class<T> clazz)  {
        //反射技术  调用无参构造方法
        String simpleName = clazz.getSimpleName();
        //接口对应的实现类全限定名
        String className = props.getProperty(simpleName);
        try {
            //得到实现类Class对象
            Class<?>  clazz1 = Class.forName(className);
            return (T)clazz1.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
